﻿@using ECG.Common
@using ECG.Utility
@model IEnumerable<ProductCategoryDto>
@{
    var itemMenu = (ProductCategoryDto)ViewBag.ItemCate;
}

<div class="cate-list-header">
    <h3>
        @{
            string cateIdFirst = itemMenu.BreadCrumb.Split('/')[1];
            var itemParent = Model.Single(m => m.Id == cateIdFirst);
        }
        <a href="@Url.Action("Index", "CategoryL1", new { categoryName = StringUtil.RemoveSpecialCharactersVn(itemParent.Name), categoryId = itemParent.Id })">
            @itemParent.Name
        </a>
    </h3>
</div>
<ul class="cateSub ul">
    @{
        var iTemParent3 = new ProductCategoryDto();
        if (itemMenu.Depth > 1)
        {
            iTemParent3 = Model.Single(m => m.Id == itemMenu.ParentId);
        }

        //--Luon hien thi cac danh muc thuoc danh muc cha tren cung
        var parentCate = "";
        if (itemMenu.Depth == 1)
        {
            parentCate = itemMenu.Id;
        }
        else if (itemMenu.Depth == 2)
        {
            var parentItem = Model.Single(m => m.Id == itemMenu.Id);
            parentCate = parentItem.ParentId;
        }
        else if (itemMenu.Depth == 3)
        {
            var parentItem = Model.Single(m => m.Id == itemMenu.Id); // lay thong tin danh muc hien tai
            var parentItem2 = Model.Single(m => m.Id == parentItem.ParentId);//lay thong tin dm cap cha
            parentCate = parentItem2.ParentId;
        }
        var childMenuList = Model.Where(m => m.ParentId == parentCate).OrderBy(m => m.Ordering);
        foreach (var item in childMenuList)
        {
            var menuListSameLevel = Model.Where(m => m.ParentId == item.Id).OrderBy(m => m.Ordering);
            <li>
                <h3>
                    <a href="@Url.Action("Index", "Category", new { categoryName = StringUtil.RemoveSpecialCharactersVn(item.Name), categoryId = item.Id })" class="lnkL2">
                        @if (item.Id == itemMenu.Id)
                        {
                            <strong>@item.Name</strong>
                        }
                        else if (itemMenu.Depth > 1)
                        {
                            if (item.Id == iTemParent3.Id)
                            {
                                <strong>@item.Name</strong>
                            }
                            else
                            {
                                @item.Name
                            }
                        }
                        else
                        {
                            @item.Name
                        }
                    </a>
                </h3>
                @if (menuListSameLevel.Any())
                {
                    var showSub = "";
                    var classSubBox = "cate-sub-box";
                    if (item.Id == itemMenu.Id && itemMenu.Depth >1)
                    {
                        classSubBox = "cate-sub-boxShow";
                        showSub = "style='display:block;'";
                    }
                    
                    if (itemMenu.Depth > 2 && item.Id == iTemParent3.Id)
                    {
                        showSub = "style='display:block;'";
                        classSubBox = "cate-sub-boxShow";
                    }
                    <div class="@classSubBox" @Html.Raw(showSub)>
                        @foreach (var item2 in menuListSameLevel)
                        {
                            <h3>
                                <a href="@Url.Action("Index", "CategoryL3", new { categoryName = StringUtil.RemoveSpecialCharactersVn(item2.Name), categoryId = item2.Id })">
                                    @if (item2.Id == itemMenu.Id)
                                    {
                                        <font color="#f60">@item2.Name</font>
                                    }
                                    else
                                    {
                                        @item2.Name
                                    }
                                </a>
                            </h3>
                        }
                    </div>
                }
            </li>
        }
    }

</ul>
